from src.main.server.mapper.impl.server_info_impl import ServerInfoImpl
from src.main.server.pojo.Result import Result


class ServerInfoController:

    def __init__(self):
        self.impl = ServerInfoImpl()

    def all_info(self):
        data = self.impl.all_info()
        return data

    def add(self, obj):
        name, place, info, start, end, person = obj
        entity_tuple = (name, place, info, start, end, person)
        self.impl.add(entity_tuple)
        return Result().success()

    def update(self, obj):
        name, place, info, start, end, person, ori_name, ori_place = obj
        entity_tuple = (name, place, info, start, end, person, ori_name, ori_place)
        self.impl.update(entity_tuple)
        return Result().success()


    def get_info(self, fd, sd):
        data = self.impl.get_info(fd, sd)
        result = data
        return result

    def delete(self, fd, sd):
        self.impl.delete(fd, sd)
        return Result().success()

    def addCount(self, data):
        """对数据库人数进行判断
           若当前人数大于等于需求人数，禁止报名
        """
        #1检查人数关系
        count = self.impl.check(data)[0]
        request_person = count[0]
        current_person = count[1]
        if current_person >= request_person:
            #当前人数大于等于需求人数，禁止报名
            return None
        self.impl.addCount(data)
        return Result().success()


if __name__ == '__main__':
    print(ServerInfoController().all_info())
    print(ServerInfoController().get_info("环保活动", "公园A"))